<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade')->comment('用户ID');
            $table->string('order_number')->unique()->comment('订单号');
            
            // 订单状态
            $table->enum('status', ['pending', 'processing', 'shipped', 'delivered', 'cancelled'])
                ->default('pending')
                ->comment('订单状态：pending-待支付，processing-待发货，shipped-已发货，delivered-已送达，cancelled-已取消');
            $table->string('extended_status', 50)->nullable()->comment('扩展状态：部分发货、退款、退货等');
            $table->enum('payment_status', ['pending', 'paid', 'failed', 'refunded', 'cod_confirmed'])
                ->default('pending')
                ->comment('支付状态：pending-待支付，paid-已支付，failed-支付失败，refunded-已退款，cod_confirmed-货到付款已确认');
            $table->string('payment_method')->nullable()->comment('支付方式：alipay-支付宝，wechat-微信，cod-货到付款');
            
            // 金额信息
            $table->decimal('total_amount', 10, 2)->default(0)->comment('订单总金额');
            $table->decimal('subtotal_amount', 10, 2)->default(0)->comment('商品小计金额');
            $table->decimal('shipping_fee', 10, 2)->default(0)->comment('运费');
            $table->decimal('discount_amount', 10, 2)->default(0)->comment('优惠金额');
            
            // 收货信息
            $table->string('receiver_name')->comment('收货人姓名');
            $table->string('receiver_phone')->comment('收货人电话');
            $table->text('receiver_address')->comment('收货人详细地址');
            
            // 订单备注
            $table->text('notes')->nullable()->comment('订单备注');
            
            // 物流信息
            $table->string('tracking_number')->nullable()->comment('物流单号');
            $table->string('shipping_company')->nullable()->comment('物流公司');
            $table->text('shipping_note')->nullable()->comment('发货备注');
            
            // 订单时间线
            $table->timestamp('paid_at')->nullable()->comment('支付时间');
            $table->timestamp('shipped_at')->nullable()->comment('发货时间');
            $table->timestamp('delivered_at')->nullable()->comment('送达时间');
            $table->timestamp('cancelled_at')->nullable()->comment('取消时间');
            $table->text('cancel_reason')->nullable()->comment('取消原因');
            
            // 状态历史
            $table->json('status_history')->nullable()->comment('状态变更历史');
            $table->timestamp('status_changed_at')->nullable()->comment('状态最后变更时间');
            $table->string('status_changed_by')->nullable()->comment('状态变更操作人');
            $table->text('status_change_reason')->nullable()->comment('状态变更原因');
            
            $table->timestamps();
            
            // 索引
            $table->index(['user_id', 'status']);
            $table->index('order_number');
            $table->index('created_at');
            $table->index('payment_status');
            $table->index('tracking_number');
            $table->index(['status', 'extended_status']);
            $table->index('status_changed_at');
            
            // 售后相关索引
            $table->index(['status', 'extended_status', 'delivered_at'], 'orders_after_sales_status_idx');
            $table->index(['delivered_at', 'status'], 'orders_delivery_after_sales_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
